<template>
  <el-dialog :title="title" :visible.sync="showDialog" @close="closeHandier">
    <el-form
      ref="form"
      :model="formData"
      label-width="120px"
      :rules="formRules"
      size="small"
    >
      <el-form-item prop="name" label="部门名称">
        <el-input
          v-model="formData.name"
          placeholder="2-10个字符"
          size="mini"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          placeholder="2-10个字符"
          size="mini"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          v-model="formData.managerId"
          placeholder="请选择负责人"
          size="mini"
          clearable
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          placeholder="1-100个字符"
          type="textarea"
          size="mini"
          :rows="4"
        />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button
              size="mini"
              type="primary"
              @click="btnOk"
            >确定</el-button>
            <el-button size="mini">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import {
  addDepartmentApi,
  delDepartmentInfoApi,
  editDepartmentApi,
  getDepartmentInfoApi,
  getDepartmentListApi,
  getManagerApi
} from '@/api/department'
export default {
  data() {
    //  检查部门编码是否重复
    const checkCodeRepeat = async(rule, value, cd) => {
      // 1.获取最新的组织架构信息
      const res = await getDepartmentListApi()
      let isExist
      if (this.formData.id) {
        isExist = res
          .filter((item) => item.id !== this.formData.id)
          .some((item) => item.code === value)
      } else {
        isExist = res.some((item) => item.code === value)
      }
      isExist ? cd(new Error('部门编码已存在')) : cd()
    }
    const checkNameRepeat = async(rule, value, cd) => {
      // 1.获取最新的组织架构信息
      const res = await getDepartmentListApi()
      let isExist
      if (this.formData.id) {
        isExist = res
          .filter((item) => item.id !== this.formData.id)
          .some((item) => item.name === value)
      } else {
        isExist = res.some((item) => item.name === value)
      }
      isExist ? cd(new Error('部门编码已存在')) : cd()
    }
    return {
      showDialog: false,
      //   表单数据变量
      formData: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      // 表单验证规则
      formRules: {
        code: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        introduce: [{ required: true, message: '请输入', trigger: 'blur' }],
        managerId: [{ required: true, message: '请输入', trigger: 'change' }],
        name: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ]
      },
      managerList: ''
      // 提交数据
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改部门' : '添加子部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    async getManagerList() {
      const res = await getManagerApi()
      this.managerList = res
    },
    async btnOk() {
      try {
        console.log(this.formData)
        await this.$refs.form.validate()
        this.formData.id
          ? await editDepartmentApi(this.formData)
          : await addDepartmentApi(this.formData)
        this.$message.success()
        this.$message.success(this.formData.id ? '修改成功' : '添加成功')
        this.showDialog = false
        this.$emit('updateList')
      } catch (error) {
        console.log(error)
      }
    },
    // ! 重置表单
    closeHandier() {
      this.formData = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
    },
    // ! 点击回显
    async replvViewData(id) {
      const res = await getDepartmentInfoApi(id)
      this.formData = res
      this.showDialog = true
    },
    // ! 删除事件
    async delRow(id) {
      try {
        await this.$confirm('确定删除')
        await delDepartmentInfoApi(id)
        this.$message.success('删除成功')
        // this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
